package com.apache.bid.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.bid.entity.BidMaxContent;
import com.apache.bid.manager.BidMaxContentManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.tools.DateUtils;

public class BidMaxContentManagerImpl implements BidMaxContentManager {

	protected IDao bidMaxContentDao;
	protected final String entityName = "com.apache.bid.entity.BidMaxContent";

	public void setBidMaxContentDao(IDao bidMaxContentDao) {
		this.bidMaxContentDao = bidMaxContentDao;
	}

	public String saveInfo(ParamsVo<BidMaxContent> vo) throws BusinessException {
		BidMaxContent bidMaxContent = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="bidMaxContent_"+infoId;
		bidMaxContent.setInfoId(infoId);
		bidMaxContent.setCreateTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
		MethodParam param = new MethodParam("BidMaxContent", cacheKey, "", entityName);
		param.setVaule(bidMaxContent);
		if (bidMaxContentDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BidMaxContent> vo) throws BusinessException {
		BidMaxContent bidMaxContent = vo.getObj();
		if (Validator.isNotNull(bidMaxContent.getInfoId())) {
			bidMaxContent.setModifyTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
			String cacheKey = "";
			//String cacheKey="bidMaxContent_"+bidMaxContent.getInfoId();
			MethodParam param = new MethodParam("BidMaxContent", cacheKey, "", entityName);
			param.setVaule(bidMaxContent);
			return bidMaxContentDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BidMaxContent> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		if (Validator.isNull(infoId)) {
			BidMaxContent bmc = vo.getObj();
			if (Validator.isEmpty(bmc) || Validator.isNull(bmc.getObjId()))
				return false;
			MethodParam param = new MethodParam("ByObject", "", "", entityName);
			param.setParams("objId", bmc.getObjId());
			if (Validator.isNotNull(bmc.getObjType()))
				param.setParams("objType", bmc.getObjType());
			if (Validator.isNotNull(bmc.getContentType()))
				param.setParams("contentType", bmc.getContentType());
			param.setDelete(true);
			return bidMaxContentDao.delete(param);
		}
		String cacheKey = "";
		//String cacheKey="bidMaxContent_"+infoId;
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		BidMaxContent info = (BidMaxContent) bidMaxContentDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("BidMaxContent");
			return bidMaxContentDao.edit(param);
		} else {
			param.setParams("infoId", infoId);
			param.setDelete(true);
			return bidMaxContentDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<BidMaxContent> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidMaxContent_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return bidMaxContentDao.selectById(param);
	}

	public Object execute(ParamsVo<BidMaxContent> vo) {
		String key = vo.getKey();
		if ("checkIsUnique".equals(key)) {
			return checkIsUnique(vo.getObj());
		}
		return null;
	}

	private boolean checkIsUnique(BidMaxContent bidMaxContent) {
		boolean mark = false;
		MethodParam param = new MethodParam("ByProperty", "", "", entityName);
		param.setParams("objId", bidMaxContent.getObjId());
		param.setParams("objType", bidMaxContent.getObjType());
		/*if ("交易公告".equals(bidMaxContent.getContentType())) {
			param.setParams("title", " = '" + bidMaxContent.getTitle() + "'");
		} else {
			param.setParams("contentType", bidMaxContent.getContentType());
		}*/
		
		String id = bidMaxContent.getInfoId();
		if(Validator.isNull(id)){
			param.setParams("title", " = '" + bidMaxContent.getTitle() + "'");
		}else{
			param.setParams("title", " = '" + bidMaxContent.getTitle() + "' and content_type != '"+bidMaxContent.getContentType()+"'");
		}
		long count = bidMaxContentDao.count(param);
		if (count == 0) {
			mark = true;
		}
		return mark;
	}

	public Page getPageInfo(ParamsVo<BidMaxContent> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by create_time desc ");
		param.setParams("orderBy", orderBy);
		Page page = bidMaxContentDao.pageSelect(param);
		return page;
	}

	public List<BidMaxContent> getList(ParamsVo<BidMaxContent> vo) {
		MethodParam param = setMethodParams(vo, 2);
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by create_time desc ");
		param.setParams("orderBy", orderBy);
		return bidMaxContentDao.select(param);
	}

	public long countInfo(ParamsVo<BidMaxContent> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return bidMaxContentDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BidMaxContent> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(bidMaxContentDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		param.setResultCache(false);
		BidMaxContent bidMaxContent = vo.getObj();
		if (Validator.isNotNull(bidMaxContent.getObjId())) {
			sb.append(" and objId = :objId");
			param.setParams("objId", bidMaxContent.getObjId());
		}
		if (Validator.isNotNull(bidMaxContent.getObjType())) {
			sb.append(" and objType = :objType");
			param.setParams("objType", bidMaxContent.getObjType());
		}
		if (Validator.isNotNull(bidMaxContent.getInfoContent())) {
			sb.append(" and infoContent = :infoContent");
			param.setParams("infoContent", bidMaxContent.getInfoContent());
		}
		if (Validator.isNotNull(bidMaxContent.getContentType())) {
			sb.append(" and contentType = :contentType");
			param.setParams("contentType", bidMaxContent.getContentType());
		}
		if (Validator.isNotNull(bidMaxContent.getIfRead())) {
			sb.append(" and ifRead = :ifRead");
			param.setParams("ifRead", bidMaxContent.getIfRead());
		}
		if (Validator.isNotNull(bidMaxContent.getSpare1())) {
			sb.append(" and spare1 = :spare1");
			param.setParams("spare1", bidMaxContent.getSpare1());
		}
		if (Validator.isNotNull(bidMaxContent.getSpare2())) {
			sb.append(" and spare2 = :spare2");
			param.setParams("spare2", bidMaxContent.getSpare2());
		}
		if (Validator.isNotNull(bidMaxContent.getSpare3())) {
			sb.append(" and spare3 = :spare3");
			param.setParams("spare3", bidMaxContent.getSpare3());
		}
		if (Validator.isNotNull(bidMaxContent.getSpare4())) {
			sb.append(" and spare4 = :spare4");
			param.setParams("spare4", bidMaxContent.getSpare4());
		}
		if (Validator.isNotNull(bidMaxContent.getSpare5())) {
			sb.append(" and spare5 = :spare5");
			param.setParams("spare5", bidMaxContent.getSpare5());
		}
		if (Validator.isNotNull(bidMaxContent.getTitle())) {
			sb.append(" and title like '%" + bidMaxContent.getTitle() + "%' ");
			param.setParams("title", " like '%" + bidMaxContent.getTitle() + "%' ");
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
